import base from './base';
import axios from './http';
import wechat  from 'weixin-js-sdk'
const apiUrl = base.test;
// 预定下单
function ConfirmOrder(params) {
    return axios.post(`${apiUrl}/nu/client/order/booking`, params);
}
// 获取订单列表
function getOrderList(currentPage, pageSize) {
    return axios.get(`${apiUrl}/nu/client/order/list/my?currentPage=${currentPage}&pageSize=${pageSize}`);
}
// 获取退换货订单列表
function getReturnOrder(currentPage, pageSize) {
    return axios.get(`${apiUrl}/nu/client/order/list/return?currentPage=${currentPage}&pageSize=${pageSize}`);
}
//获取订单详情
function getOrderDetail(orderId) {
    return axios.get(`${apiUrl}/nu/client/order/${orderId}`);
}
//申请退换货订单
function applyRefundOrder(orderId) {
    return axios.get(`${apiUrl}/nu/client/order/return/create?orderId=${orderId}`);
}

// 取消订单
function cancelOrder(orderId) {
    return axios.get(`${apiUrl}/nu/client/order/cancel?orderId=${orderId}`);
}
// 关联订单凭证图片
function uploadOrderImg(orderId, imgPath) {
    return axios.get(`${apiUrl}/nu/client/order/relate/img?orderId=${orderId}&imgPath=${imgPath}`);
}

function checkOrder(orderId, resultType) {
   return axios.post(`${apiUrl}/nu/client/order/verify/sub?orderId=${orderId}&resultType=${resultType}`);
}
// 微信支付
function setwechat(toUrl) {
    // 获取jssdk配置
    axios.post(`${apiUrl}/nu/anon/user/wechat/jsConfigParam`, {url: toUrl, number: 1}).then(res => {
        if (res && res.code === 0) {
            let shareObj = res.data
            wechat.config({
                debug: false, // 开启debug
                appId: shareObj.appId, // 微信appid
                timestamp: shareObj.timestamp, // 时间戳
                nonceStr: shareObj.nonceStr,
                signature: shareObj.signature, // 签名
                jsApiList: ['chooseWXPay', 'updateAppMessageShareData', 'updateTimelineShareData'] // 使用的api
            })
        }
    }, reject => {

    })
}

// 微信
function wechatPay(payOrderNo) {
    setwechat(window.location.href);
    axios.get(`${apiUrl}/nu/order/pay/wechat?payOrderNo=${payOrderNo}`).then(res => {
        if (res && res.code === 0) {
            let data = res.data
            wechat.ready(function () {
                wechat.chooseWXPay({
                    timestamp: data.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
                    nonceStr: data.nonceStr, // 支付签名随机串，不长于 32 位
                    package: data.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）
                    signType: data.signType, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
                    paySign: data.paySign, // 支付签名
                    success: function (res) {
                        // 支付成功后的回调函数
                        window.location.href = `${apiUrl}/myOrder.html`
                    },
                    fail: function () {
                        alert('支付失败！')
                        window.location.href = `${apiUrl}/myOrder.html`
                    }
                });
            })
        }
    }, reject => {

    })
}

function wechatShare(title = '', link, desc = '', imgUrl = '') {
    link = window.location.href
    // 获取jssdk配置
    axios.post(`${apiUrl}/nu/anon/user/wechat/jsConfigParam`, {url: link, number: 1}).then(res => {
        if (res && res.code === 0) {
            let shareObj = res.data
            wx.config({
                debug: false, // 开启debug
                appId: shareObj.appId, // 微信appid
                timestamp: shareObj.timestamp, // 时间戳
                nonceStr: shareObj.nonceStr,
                signature: shareObj.signature, // 签名
                jsApiList: ['onMenuShareAppMessage', 'onMenuShareTimeline','updateAppMessageShareData', 'updateTimelineShareData'] // 使用的api
            })
        }
    }, reject => {

    })
    wx.ready(function () {   //需在用户可能点击分享按钮前就先调用
        console.log(title, link)
        wx.onMenuShareAppMessage({
            title: title,
            desc: '不忘初心，与爱同行',
            imgUrl: 'http://www.nineunity.com/upload/20190105/jpg/d0afddf8-3804-45fd-b125-03713dea36eb.jpg',
            type: 'link', 
            dataUrl: '',
            success: function () { 
               console.log(' onMenuShareAppMessage 分享成功～');
            },
            cancel: function () { 
                console.log(' onMenuShareAppMessage 分享失败');
            }
        });
        //分享到朋友圈
        wx.onMenuShareTimeline({
            title: title,
            desc: '不忘初心，与爱同行',
            imgUrl: 'http://www.nineunity.com/upload/20190105/jpg/d0afddf8-3804-45fd-b125-03713dea36eb.jpg',
            success: function () { 
               console.log(' onMenuShareTimeline 分享成功～');
            },
        });
        // wx.updateAppMessageShareData({
        //     title: title, // 分享标题
        //     desc: desc, // 分享描述
        //     link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        //     imgUrl: '', // 分享图标
        //     success: function () {
        //         // 设置成功
        //         console.log('updateAppMessageShareData 设置成功');
        //     }
        // });
        // wx.updateTimelineShareData({
        //     title: title, // 分享标题
        //     link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        //     imgUrl: '', // 分享图标
        //     success: function () {
        //         // 设置成功
        //     }
        // })
    })
}

// 会员订单接口 
function getMemberOrder(currentPage, pageSize, type) {
    return axios.get(`${apiUrl}/nu/client/order/list/sub?type=${type}&currentPage=${currentPage}&pageSize=${pageSize}`);
}

export {
    ConfirmOrder,
    getOrderList,
    getOrderDetail,
    cancelOrder,
    applyRefundOrder,
    uploadOrderImg,
    wechatPay,
    setwechat,
    wechatShare,
    getMemberOrder,
    getReturnOrder,
    checkOrder
}

